<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8">
    
    <title>Scalars &mdash; NumPy v1.18 Manual</title>
    
    <link rel="stylesheet" type="text/css" href="../_static/css/spc-bootstrap.css">
    <link rel="stylesheet" type="text/css" href="../_static/css/spc-extend.css">
    <link rel="stylesheet" href="../_static/scipy.css" type="text/css" >
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" >
    <link rel="stylesheet" href="../_static/graphviz.css" type="text/css" >
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '1.18.1',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  false
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    <script type="text/javascript" src="../_static/js/copybutton.js"></script>
    <link rel="author" title="About these documents" href="../about.html" >
    <link rel="index" title="Index" href="../genindex.html" >
    <link rel="search" title="Search" href="../search.html" >
    <link rel="top" title="NumPy v1.18 Manual" href="../index.html" >
    <link rel="up" title="Array objects" href="arrays.html" >
    <link rel="next" title="numpy.generic.flags" href="generated/numpy.generic.flags.html" >
    <link rel="prev" title="numpy.ndarray.__repr__" href="generated/numpy.ndarray.__repr__.html" > 
  </head>
  <body>
<div class="container">
  <div class="top-scipy-org-logo-header" style="background-color: #a2bae8;">
    <a href="../index.html">
      <img border=0 alt="NumPy" src="../_static/numpy_logo.png"></a>
    </div>
  </div>
</div>


    <div class="container">
      <div class="main">
        
	<div class="row-fluid">
	  <div class="span12">
	    <div class="spc-navbar">
              
    <ul class="nav nav-pills pull-left">
        <li class="active"><a href="https://numpy.org/">NumPy.org</a></li>
        <li class="active"><a href="https://numpy.org/doc">Docs</a></li>
        
        <li class="active"><a href="../index.html">NumPy v1.18 Manual</a></li>
        

          <li class="active"><a href="index.html" >NumPy Reference</a></li>
          <li class="active"><a href="arrays.html" accesskey="U">Array objects</a></li> 
    </ul>
              
              
    <ul class="nav nav-pills pull-right">
      <li class="active">
        <a href="../genindex.html" title="General Index"
           accesskey="I">index</a>
      </li>
      <li class="active">
        <a href="generated/numpy.generic.flags.html" title="numpy.generic.flags"
           accesskey="N">next</a>
      </li>
      <li class="active">
        <a href="generated/numpy.ndarray.__repr__.html" title="numpy.ndarray.__repr__"
           accesskey="P">previous</a>
      </li>
    </ul>
              
	    </div>
	  </div>
	</div>
        

	<div class="row-fluid">
      <div class="spc-rightsidebar span3">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Scalars</a><ul>
<li><a class="reference internal" href="#built-in-scalar-types">Built-in scalar types</a></li>
<li><a class="reference internal" href="#attributes">Attributes</a></li>
<li><a class="reference internal" href="#indexing">Indexing</a></li>
<li><a class="reference internal" href="#methods">Methods</a></li>
<li><a class="reference internal" href="#defining-new-types">Defining new types</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="generated/numpy.ndarray.__repr__.html"
                        title="previous chapter">numpy.ndarray.__repr__</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="generated/numpy.generic.flags.html"
                        title="next chapter">numpy.generic.flags</a></p>
<div id="searchbox" style="display: none" role="search">
  <h4>Quick search</h4>
    <div>
    <form class="search" action="../search.html" method="get">
      <input type="text" style="width: inherit;" name="q" />
      <input type="submit" value="search" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
          <div class="span9">
            
        <div class="bodywrapper">
          <div class="body" id="spc-section-body">
            
  <div class="section" id="scalars">
<span id="arrays-scalars"></span><h1>Scalars<a class="headerlink" href="#scalars" title="Permalink to this headline">¶</a></h1>
<p>Python defines only one type of a particular data class (there is only
one integer type, one floating-point type, etc.). This can be
convenient in applications that don’t need to be concerned with all
the ways data can be represented in a computer.  For scientific
computing, however, more control is often needed.</p>
<p>In NumPy, there are 24 new fundamental Python types to describe
different types of scalars. These type descriptors are mostly based on
the types available in the C language that CPython is written in, with
several additional types compatible with Python’s types.</p>
<p>Array scalars have the same attributes and methods as <a class="reference internal" href="generated/numpy.ndarray.html#numpy.ndarray" title="numpy.ndarray"><code class="xref py py-class docutils literal notranslate"><span class="pre">ndarrays</span></code></a>. <a class="footnote-reference brackets" href="#id2" id="id1">1</a> This allows one to treat items of an array partly on
the same footing as arrays, smoothing out rough edges that result when
mixing scalar and array operations.</p>
<p>Array scalars live in a hierarchy (see the Figure below) of data
types. They can be detected using the hierarchy: For example,
<code class="docutils literal notranslate"><span class="pre">isinstance(val,</span> <span class="pre">np.generic)</span></code> will return <code class="xref py py-const docutils literal notranslate"><span class="pre">True</span></code> if <em>val</em> is
an array scalar object. Alternatively, what kind of array scalar is
present can be determined using other members of the data type
hierarchy. Thus, for example <code class="docutils literal notranslate"><span class="pre">isinstance(val,</span> <span class="pre">np.complexfloating)</span></code>
will return <code class="xref py py-const docutils literal notranslate"><span class="pre">True</span></code> if <em>val</em> is a complex valued type, while
<code class="xref py py-const docutils literal notranslate"><span class="pre">isinstance(val,</span> <span class="pre">np.flexible)</span></code> will return true if <em>val</em> is one
of the flexible itemsize array types (<code class="xref py py-class docutils literal notranslate"><span class="pre">string</span></code>,
<code class="xref py py-class docutils literal notranslate"><span class="pre">unicode</span></code>, <code class="xref py py-class docutils literal notranslate"><span class="pre">void</span></code>).</p>
<div class="figure align-default" id="id3">
<img alt="../_images/dtype-hierarchy.png" src="../_images/dtype-hierarchy.png" />
<p class="caption"><span class="caption-text"><strong>Figure:</strong> Hierarchy of type objects representing the array data
types. Not shown are the two integer types <code class="xref py py-class docutils literal notranslate"><span class="pre">intp</span></code> and
<code class="xref py py-class docutils literal notranslate"><span class="pre">uintp</span></code> which just point to the integer type that holds a
pointer for the platform. All the number types can be obtained
using bit-width names as well.</span><a class="headerlink" href="#id3" title="Permalink to this image">¶</a></p>
</div>
<dl class="footnote brackets">
<dt class="label" id="id2"><span class="brackets"><a class="fn-backref" href="#id1">1</a></span></dt>
<dd><p>However, array scalars are immutable, so none of the array
scalar attributes are settable.</p>
</dd>
</dl>
<div class="section" id="built-in-scalar-types">
<span id="arrays-scalars-built-in"></span><span id="arrays-scalars-character-codes"></span><h2>Built-in scalar types<a class="headerlink" href="#built-in-scalar-types" title="Permalink to this headline">¶</a></h2>
<p>The built-in scalar types are shown below. Along with their (mostly)
C-derived names, the integer, float, and complex data-types are also
available using a bit-width convention so that an array of the right
size can always be ensured (e.g. <code class="xref py py-class docutils literal notranslate"><span class="pre">int8</span></code>, <code class="xref py py-class docutils literal notranslate"><span class="pre">float64</span></code>,
<code class="xref py py-class docutils literal notranslate"><span class="pre">complex128</span></code>). Two aliases (<code class="xref py py-class docutils literal notranslate"><span class="pre">intp</span></code> and <code class="xref py py-class docutils literal notranslate"><span class="pre">uintp</span></code>)
pointing to the integer type that is sufficiently large to hold a C pointer
are also provided. The C-like names are associated with character codes,
which are shown in the table. Use of the character codes, however,
is discouraged.</p>
<p>Some of the scalar types are essentially equivalent to fundamental
Python types and therefore inherit from them as well as from the
generic array scalar type:</p>
<table class="docutils align-default">
<colgroup>
<col style="width: 38%" />
<col style="width: 62%" />
</colgroup>
<thead>
<tr class="row-odd"><th class="head"><p>Array scalar type</p></th>
<th class="head"><p>Related Python type</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">int_</span></code></p></td>
<td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">IntType</span></code> (Python 2 only)</p></td>
</tr>
<tr class="row-odd"><td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">float_</span></code></p></td>
<td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">FloatType</span></code></p></td>
</tr>
<tr class="row-even"><td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">complex_</span></code></p></td>
<td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">ComplexType</span></code></p></td>
</tr>
<tr class="row-odd"><td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes_</span></code></p></td>
<td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">BytesType</span></code></p></td>
</tr>
<tr class="row-even"><td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">unicode_</span></code></p></td>
<td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">UnicodeType</span></code></p></td>
</tr>
</tbody>
</table>
<p>The <code class="xref py py-class docutils literal notranslate"><span class="pre">bool_</span></code> data type is very similar to the Python
<code class="xref py py-class docutils literal notranslate"><span class="pre">BooleanType</span></code> but does not inherit from it because Python’s
<code class="xref py py-class docutils literal notranslate"><span class="pre">BooleanType</span></code> does not allow itself to be inherited from, and
on the C-level the size of the actual bool data is not the same as a
Python Boolean scalar.</p>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>The <code class="xref py py-class docutils literal notranslate"><span class="pre">bool_</span></code> type is not a subclass of the <code class="xref py py-class docutils literal notranslate"><span class="pre">int_</span></code> type
(the <code class="xref py py-class docutils literal notranslate"><span class="pre">bool_</span></code> is not even a number type). This is different
than Python’s default implementation of <a class="reference external" href="https://docs.python.org/dev/library/functions.html#bool" title="(in Python v3.9)"><code class="xref py py-class docutils literal notranslate"><span class="pre">bool</span></code></a> as a
sub-class of int.</p>
</div>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>The <code class="xref py py-class docutils literal notranslate"><span class="pre">int_</span></code> type does <strong>not</strong> inherit from the
<a class="reference external" href="https://docs.python.org/dev/library/functions.html#int" title="(in Python v3.9)"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a> built-in under Python 3, because type <a class="reference external" href="https://docs.python.org/dev/library/functions.html#int" title="(in Python v3.9)"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a> is no
longer a fixed-width integer type.</p>
</div>
<div class="admonition tip">
<p class="admonition-title">Tip</p>
<p>The default data type in NumPy is <code class="xref py py-class docutils literal notranslate"><span class="pre">float_</span></code>.</p>
</div>
<p>In the tables below, <code class="docutils literal notranslate"><span class="pre">platform?</span></code> means that the type may not be
available on all platforms. Compatibility with different C or Python
types is indicated: two types are compatible if their data is of the
same size and interpreted in the same way.</p>
<p>Booleans:</p>
<table class="docutils align-default">
<colgroup>
<col style="width: 30%" />
<col style="width: 46%" />
<col style="width: 24%" />
</colgroup>
<thead>
<tr class="row-odd"><th class="head"><p>Type</p></th>
<th class="head"><p>Remarks</p></th>
<th class="head"><p>Character code</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">bool_</span></code></p></td>
<td><p>compatible: Python bool</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'?'</span></code></p></td>
</tr>
<tr class="row-odd"><td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">bool8</span></code></p></td>
<td><p>8 bits</p></td>
<td></td>
</tr>
</tbody>
</table>
<p>Integers:</p>
<table class="docutils align-default">
<colgroup>
<col style="width: 30%" />
<col style="width: 46%" />
<col style="width: 24%" />
</colgroup>
<tbody>
<tr class="row-odd"><td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">byte</span></code></p></td>
<td><p>compatible: C char</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'b'</span></code></p></td>
</tr>
<tr class="row-even"><td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">short</span></code></p></td>
<td><p>compatible: C short</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'h'</span></code></p></td>
</tr>
<tr class="row-odd"><td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">intc</span></code></p></td>
<td><p>compatible: C int</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'i'</span></code></p></td>
</tr>
<tr class="row-even"><td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">int_</span></code></p></td>
<td><p>compatible: Python int</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'l'</span></code></p></td>
</tr>
<tr class="row-odd"><td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">longlong</span></code></p></td>
<td><p>compatible: C long long</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'q'</span></code></p></td>
</tr>
<tr class="row-even"><td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">intp</span></code></p></td>
<td><p>large enough to fit a pointer</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'p'</span></code></p></td>
</tr>
<tr class="row-odd"><td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">int8</span></code></p></td>
<td><p>8 bits</p></td>
<td></td>
</tr>
<tr class="row-even"><td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">int16</span></code></p></td>
<td><p>16 bits</p></td>
<td></td>
</tr>
<tr class="row-odd"><td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">int32</span></code></p></td>
<td><p>32 bits</p></td>
<td></td>
</tr>
<tr class="row-even"><td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">int64</span></code></p></td>
<td><p>64 bits</p></td>
<td></td>
</tr>
</tbody>
</table>
<p>Unsigned integers:</p>
<table class="docutils align-default">
<colgroup>
<col style="width: 30%" />
<col style="width: 46%" />
<col style="width: 24%" />
</colgroup>
<tbody>
<tr class="row-odd"><td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">ubyte</span></code></p></td>
<td><p>compatible: C unsigned char</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'B'</span></code></p></td>
</tr>
<tr class="row-even"><td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">ushort</span></code></p></td>
<td><p>compatible: C unsigned short</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'H'</span></code></p></td>
</tr>
<tr class="row-odd"><td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">uintc</span></code></p></td>
<td><p>compatible: C unsigned int</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'I'</span></code></p></td>
</tr>
<tr class="row-even"><td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">uint</span></code></p></td>
<td><p>compatible: Python int</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'L'</span></code></p></td>
</tr>
<tr class="row-odd"><td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">ulonglong</span></code></p></td>
<td><p>compatible: C long long</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'Q'</span></code></p></td>
</tr>
<tr class="row-even"><td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">uintp</span></code></p></td>
<td><p>large enough to fit a pointer</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'P'</span></code></p></td>
</tr>
<tr class="row-odd"><td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">uint8</span></code></p></td>
<td><p>8 bits</p></td>
<td></td>
</tr>
<tr class="row-even"><td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">uint16</span></code></p></td>
<td><p>16 bits</p></td>
<td></td>
</tr>
<tr class="row-odd"><td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">uint32</span></code></p></td>
<td><p>32 bits</p></td>
<td></td>
</tr>
<tr class="row-even"><td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">uint64</span></code></p></td>
<td><p>64 bits</p></td>
<td></td>
</tr>
</tbody>
</table>
<p>Floating-point numbers:</p>
<table class="docutils align-default">
<colgroup>
<col style="width: 30%" />
<col style="width: 46%" />
<col style="width: 24%" />
</colgroup>
<tbody>
<tr class="row-odd"><td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">half</span></code></p></td>
<td></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'e'</span></code></p></td>
</tr>
<tr class="row-even"><td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">single</span></code></p></td>
<td><p>compatible: C float</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'f'</span></code></p></td>
</tr>
<tr class="row-odd"><td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">double</span></code></p></td>
<td><p>compatible: C double</p></td>
<td></td>
</tr>
<tr class="row-even"><td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">float_</span></code></p></td>
<td><p>compatible: Python float</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'d'</span></code></p></td>
</tr>
<tr class="row-odd"><td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">longfloat</span></code></p></td>
<td><p>compatible: C long float</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'g'</span></code></p></td>
</tr>
<tr class="row-even"><td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">float16</span></code></p></td>
<td><p>16 bits</p></td>
<td></td>
</tr>
<tr class="row-odd"><td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">float32</span></code></p></td>
<td><p>32 bits</p></td>
<td></td>
</tr>
<tr class="row-even"><td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">float64</span></code></p></td>
<td><p>64 bits</p></td>
<td></td>
</tr>
<tr class="row-odd"><td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">float96</span></code></p></td>
<td><p>96 bits, platform?</p></td>
<td></td>
</tr>
<tr class="row-even"><td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">float128</span></code></p></td>
<td><p>128 bits, platform?</p></td>
<td></td>
</tr>
</tbody>
</table>
<p>Complex floating-point numbers:</p>
<table class="docutils align-default">
<colgroup>
<col style="width: 30%" />
<col style="width: 46%" />
<col style="width: 24%" />
</colgroup>
<tbody>
<tr class="row-odd"><td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">csingle</span></code></p></td>
<td></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'F'</span></code></p></td>
</tr>
<tr class="row-even"><td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">complex_</span></code></p></td>
<td><p>compatible: Python complex</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'D'</span></code></p></td>
</tr>
<tr class="row-odd"><td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">clongfloat</span></code></p></td>
<td></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'G'</span></code></p></td>
</tr>
<tr class="row-even"><td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">complex64</span></code></p></td>
<td><p>two 32-bit floats</p></td>
<td></td>
</tr>
<tr class="row-odd"><td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">complex128</span></code></p></td>
<td><p>two 64-bit floats</p></td>
<td></td>
</tr>
<tr class="row-even"><td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">complex192</span></code></p></td>
<td><p>two 96-bit floats,
platform?</p></td>
<td></td>
</tr>
<tr class="row-odd"><td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">complex256</span></code></p></td>
<td><p>two 128-bit floats,
platform?</p></td>
<td></td>
</tr>
</tbody>
</table>
<p>Any Python object:</p>
<table class="docutils align-default">
<colgroup>
<col style="width: 30%" />
<col style="width: 46%" />
<col style="width: 24%" />
</colgroup>
<tbody>
<tr class="row-odd"><td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">object_</span></code></p></td>
<td><p>any Python object</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'O'</span></code></p></td>
</tr>
</tbody>
</table>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>The data actually stored in object arrays
(<em>i.e.</em>, arrays having dtype <code class="xref py py-class docutils literal notranslate"><span class="pre">object_</span></code>) are references to
Python objects, not the objects themselves. Hence, object arrays
behave more like usual Python <a class="reference external" href="https://docs.python.org/dev/library/stdtypes.html#list" title="(in Python v3.9)"><code class="xref py py-class docutils literal notranslate"><span class="pre">lists</span></code></a>, in the sense
that their contents need not be of the same Python type.</p>
<p>The object type is also special because an array containing
<code class="xref py py-class docutils literal notranslate"><span class="pre">object_</span></code> items does not return an <code class="xref py py-class docutils literal notranslate"><span class="pre">object_</span></code> object
on item access, but instead returns the actual object that
the array item refers to.</p>
</div>
<p id="index-0">The following data types are <strong>flexible</strong>: they have no predefined
size and the data they describe can be of different length in different
arrays. (In the character codes <code class="docutils literal notranslate"><span class="pre">#</span></code> is an integer denoting how many
elements the data type consists of.)</p>
<table class="docutils align-default">
<colgroup>
<col style="width: 33%" />
<col style="width: 53%" />
<col style="width: 14%" />
</colgroup>
<tbody>
<tr class="row-odd"><td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes_</span></code></p></td>
<td><p>compatible: Python bytes</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'S#'</span></code></p></td>
</tr>
<tr class="row-even"><td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">unicode_</span></code></p></td>
<td><p>compatible: Python unicode/str</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'U#'</span></code></p></td>
</tr>
<tr class="row-odd"><td><p><code class="xref py py-class docutils literal notranslate"><span class="pre">void</span></code></p></td>
<td></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'V#'</span></code></p></td>
</tr>
</tbody>
</table>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>See <a class="reference internal" href="arrays.dtypes.html#string-dtype-note"><span class="std std-ref">Note on string types</span></a>.</p>
<p>Numeric Compatibility: If you used old typecode characters in your
Numeric code (which was never recommended), you will need to change
some of them to the new characters. In particular, the needed
changes are <code class="docutils literal notranslate"><span class="pre">c</span> <span class="pre">-&gt;</span> <span class="pre">S1</span></code>, <code class="docutils literal notranslate"><span class="pre">b</span> <span class="pre">-&gt;</span> <span class="pre">B</span></code>, <code class="docutils literal notranslate"><span class="pre">1</span> <span class="pre">-&gt;</span> <span class="pre">b</span></code>, <code class="docutils literal notranslate"><span class="pre">s</span> <span class="pre">-&gt;</span> <span class="pre">h</span></code>, <code class="docutils literal notranslate"><span class="pre">w</span> <span class="pre">-&gt;</span>
<span class="pre">H</span></code>, and <code class="docutils literal notranslate"><span class="pre">u</span> <span class="pre">-&gt;</span> <span class="pre">I</span></code>. These changes make the type character
convention more consistent with other Python modules such as the
<a class="reference external" href="https://docs.python.org/dev/library/struct.html#module-struct" title="(in Python v3.9)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">struct</span></code></a> module.</p>
</div>
</div>
<div class="section" id="attributes">
<h2>Attributes<a class="headerlink" href="#attributes" title="Permalink to this headline">¶</a></h2>
<p>The array scalar objects have an <code class="xref py py-obj docutils literal notranslate"><span class="pre">array</span> <span class="pre">priority</span></code> of <a class="reference internal" href="c-api/array.html#c.NPY_SCALAR_PRIORITY" title="NPY_SCALAR_PRIORITY"><code class="xref c c-data docutils literal notranslate"><span class="pre">NPY_SCALAR_PRIORITY</span></code></a>
(-1,000,000.0). They also do not (yet) have a <a class="reference internal" href="generated/numpy.ndarray.ctypes.html#numpy.ndarray.ctypes" title="numpy.ndarray.ctypes"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ctypes</span></code></a>
attribute. Otherwise, they share the same attributes as arrays:</p>
<table class="longtable docutils align-default">
<colgroup>
<col style="width: 10%" />
<col style="width: 90%" />
</colgroup>
<tbody>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/numpy.generic.flags.html#numpy.generic.flags" title="numpy.generic.flags"><code class="xref py py-obj docutils literal notranslate"><span class="pre">generic.flags</span></code></a></p></td>
<td><p>integer value of flags</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="generated/numpy.generic.shape.html#numpy.generic.shape" title="numpy.generic.shape"><code class="xref py py-obj docutils literal notranslate"><span class="pre">generic.shape</span></code></a></p></td>
<td><p>tuple of array dimensions</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/numpy.generic.strides.html#numpy.generic.strides" title="numpy.generic.strides"><code class="xref py py-obj docutils literal notranslate"><span class="pre">generic.strides</span></code></a></p></td>
<td><p>tuple of bytes steps in each dimension</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="generated/numpy.generic.ndim.html#numpy.generic.ndim" title="numpy.generic.ndim"><code class="xref py py-obj docutils literal notranslate"><span class="pre">generic.ndim</span></code></a></p></td>
<td><p>number of array dimensions</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/numpy.generic.data.html#numpy.generic.data" title="numpy.generic.data"><code class="xref py py-obj docutils literal notranslate"><span class="pre">generic.data</span></code></a></p></td>
<td><p>pointer to start of data</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="generated/numpy.generic.size.html#numpy.generic.size" title="numpy.generic.size"><code class="xref py py-obj docutils literal notranslate"><span class="pre">generic.size</span></code></a></p></td>
<td><p>number of elements in the gentype</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/numpy.generic.itemsize.html#numpy.generic.itemsize" title="numpy.generic.itemsize"><code class="xref py py-obj docutils literal notranslate"><span class="pre">generic.itemsize</span></code></a></p></td>
<td><p>length of one element in bytes</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="generated/numpy.generic.base.html#numpy.generic.base" title="numpy.generic.base"><code class="xref py py-obj docutils literal notranslate"><span class="pre">generic.base</span></code></a></p></td>
<td><p>base object</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/numpy.generic.dtype.html#numpy.generic.dtype" title="numpy.generic.dtype"><code class="xref py py-obj docutils literal notranslate"><span class="pre">generic.dtype</span></code></a></p></td>
<td><p>get array data-descriptor</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="generated/numpy.generic.real.html#numpy.generic.real" title="numpy.generic.real"><code class="xref py py-obj docutils literal notranslate"><span class="pre">generic.real</span></code></a></p></td>
<td><p>real part of scalar</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/numpy.generic.imag.html#numpy.generic.imag" title="numpy.generic.imag"><code class="xref py py-obj docutils literal notranslate"><span class="pre">generic.imag</span></code></a></p></td>
<td><p>imaginary part of scalar</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="generated/numpy.generic.flat.html#numpy.generic.flat" title="numpy.generic.flat"><code class="xref py py-obj docutils literal notranslate"><span class="pre">generic.flat</span></code></a></p></td>
<td><p>a 1-d view of scalar</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/numpy.generic.T.html#numpy.generic.T" title="numpy.generic.T"><code class="xref py py-obj docutils literal notranslate"><span class="pre">generic.T</span></code></a></p></td>
<td><p>transpose</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="generated/numpy.generic.__array_interface__.html#numpy.generic.__array_interface__" title="numpy.generic.__array_interface__"><code class="xref py py-obj docutils literal notranslate"><span class="pre">generic.__array_interface__</span></code></a></p></td>
<td><p>Array protocol: Python side</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/numpy.generic.__array_struct__.html#numpy.generic.__array_struct__" title="numpy.generic.__array_struct__"><code class="xref py py-obj docutils literal notranslate"><span class="pre">generic.__array_struct__</span></code></a></p></td>
<td><p>Array protocol: struct</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="generated/numpy.generic.__array_priority__.html#numpy.generic.__array_priority__" title="numpy.generic.__array_priority__"><code class="xref py py-obj docutils literal notranslate"><span class="pre">generic.__array_priority__</span></code></a></p></td>
<td><p>Array priority.</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/numpy.generic.__array_wrap__.html#numpy.generic.__array_wrap__" title="numpy.generic.__array_wrap__"><code class="xref py py-obj docutils literal notranslate"><span class="pre">generic.__array_wrap__</span></code></a>()</p></td>
<td><p>sc.__array_wrap__(obj) return scalar from array</p></td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="indexing">
<h2>Indexing<a class="headerlink" href="#indexing" title="Permalink to this headline">¶</a></h2>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="arrays.indexing.html#arrays-indexing"><span class="std std-ref">Indexing</span></a>, <a class="reference internal" href="arrays.dtypes.html#arrays-dtypes"><span class="std std-ref">Data type objects (dtype)</span></a></p>
</div>
<p>Array scalars can be indexed like 0-dimensional arrays: if <em>x</em> is an
array scalar,</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">x[()]</span></code> returns a copy of array scalar</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">x[...]</span></code> returns a 0-dimensional <a class="reference internal" href="generated/numpy.ndarray.html#numpy.ndarray" title="numpy.ndarray"><code class="xref py py-class docutils literal notranslate"><span class="pre">ndarray</span></code></a></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">x['field-name']</span></code> returns the array scalar in the field <em>field-name</em>.
(<em>x</em> can have fields, for example, when it corresponds to a structured data type.)</p></li>
</ul>
</div>
<div class="section" id="methods">
<h2>Methods<a class="headerlink" href="#methods" title="Permalink to this headline">¶</a></h2>
<p>Array scalars have exactly the same methods as arrays. The default
behavior of these methods is to internally convert the scalar to an
equivalent 0-dimensional array and to call the corresponding array
method. In addition, math operations on array scalars are defined so
that the same hardware flags are set and used to interpret the results
as for <a class="reference internal" href="ufuncs.html#ufuncs"><span class="std std-ref">ufunc</span></a>, so that the error state used for ufuncs
also carries over to the math on array scalars.</p>
<p>The exceptions to the above rules are given below:</p>
<table class="longtable docutils align-default">
<colgroup>
<col style="width: 10%" />
<col style="width: 90%" />
</colgroup>
<tbody>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/numpy.generic.html#numpy.generic" title="numpy.generic"><code class="xref py py-obj docutils literal notranslate"><span class="pre">generic</span></code></a></p></td>
<td><p>Base class for numpy scalar types.</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="generated/numpy.generic.__array__.html#numpy.generic.__array__" title="numpy.generic.__array__"><code class="xref py py-obj docutils literal notranslate"><span class="pre">generic.__array__</span></code></a>()</p></td>
<td><p>sc.__array__(dtype) return 0-dim array from scalar with specified dtype</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/numpy.generic.__array_wrap__.html#numpy.generic.__array_wrap__" title="numpy.generic.__array_wrap__"><code class="xref py py-obj docutils literal notranslate"><span class="pre">generic.__array_wrap__</span></code></a>()</p></td>
<td><p>sc.__array_wrap__(obj) return scalar from array</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="generated/numpy.generic.squeeze.html#numpy.generic.squeeze" title="numpy.generic.squeeze"><code class="xref py py-obj docutils literal notranslate"><span class="pre">generic.squeeze</span></code></a>()</p></td>
<td><p>Not implemented (virtual attribute)</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/numpy.generic.byteswap.html#numpy.generic.byteswap" title="numpy.generic.byteswap"><code class="xref py py-obj docutils literal notranslate"><span class="pre">generic.byteswap</span></code></a>()</p></td>
<td><p>Not implemented (virtual attribute)</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="generated/numpy.generic.__reduce__.html#numpy.generic.__reduce__" title="numpy.generic.__reduce__"><code class="xref py py-obj docutils literal notranslate"><span class="pre">generic.__reduce__</span></code></a>()</p></td>
<td><p>helper for pickle</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/numpy.generic.__setstate__.html#numpy.generic.__setstate__" title="numpy.generic.__setstate__"><code class="xref py py-obj docutils literal notranslate"><span class="pre">generic.__setstate__</span></code></a>()</p></td>
<td><p></p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="generated/numpy.generic.setflags.html#numpy.generic.setflags" title="numpy.generic.setflags"><code class="xref py py-obj docutils literal notranslate"><span class="pre">generic.setflags</span></code></a>()</p></td>
<td><p>Not implemented (virtual attribute)</p></td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="defining-new-types">
<h2>Defining new types<a class="headerlink" href="#defining-new-types" title="Permalink to this headline">¶</a></h2>
<p>There are two ways to effectively define a new array scalar type
(apart from composing structured types <a class="reference internal" href="arrays.dtypes.html#arrays-dtypes"><span class="std std-ref">dtypes</span></a> from
the built-in scalar types): One way is to simply subclass the
<a class="reference internal" href="generated/numpy.ndarray.html#numpy.ndarray" title="numpy.ndarray"><code class="xref py py-class docutils literal notranslate"><span class="pre">ndarray</span></code></a> and overwrite the methods of interest. This will work to
a degree, but internally certain behaviors are fixed by the data type of
the array.  To fully customize the data type of an array you need to
define a new data-type, and register it with NumPy. Such new types can only
be defined in C, using the <a class="reference internal" href="c-api/index.html#c-api"><span class="std std-ref">NumPy C-API</span></a>.</p>
</div>
</div>


          </div>
        </div>
          </div>
        </div>
      </div>
    </div>

    <div class="container container-navbar-bottom">
      <div class="spc-navbar">
        
      </div>
    </div>
    <div class="container">
    <div class="footer">
    <div class="row-fluid">
    <ul class="inline pull-left">
      <li>
        &copy; Copyright 2008-2019, The SciPy community.
      </li>
      <li>
      Last updated on Feb 20, 2020.
      </li>
      <li>
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 2.4.2.
      </li>
    </ul>
    </div>
    </div>
    </div>
  </body>
</html>